<%@ page import="com.kma.ncpractice2013.dao.UserDAO" %>
<%@ page import="com.kma.ncpractice2013.model.User" %>
<%@ page import="org.json.JSONObject" %>
<%@ page import="com.kma.ncpractice2013.model.Order" %>
<%@ page import="java.util.List" %>
<%@ page import="com.kma.ncpractice2013.model.Location" %>
<%@ page import="com.kma.ncpractice2013.dao.OrderDAO" %>
<%@ page import="com.kma.ncpractice2013.dao.LocationDAO" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.kma.ncpractice2013.model.Hotel" %>
<%@ page import="com.kma.ncpractice2013.dao.HotelDAO" %>
<%

    User user = (User)session.getAttribute("user");
	String group = JSONObject.valueToString(user.getGroupId().toString());
	String access = JSONObject.valueToString(user.getAccessLevelId().toString());
    JSONObject jsonObject = new JSONObject(user);
    String ts = jsonObject.toString();

    OrderDAO orderDAO = new OrderDAO();
    HotelDAO hotelDAO = new HotelDAO();
    List<Order> orderList = orderDAO.getByUser(user.getId());
    List<Hotel> hotels = new ArrayList<Hotel>();


    for (Order order: orderList)
    {

        hotels.add(hotelDAO.getByHotelId(order.getLocationId()));

    }
    String hotel_list = JSONObject.valueToString(hotels);
    String order_list = JSONObject.valueToString(orderList);

%>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>User profile</title>
<link rel="stylesheet" href="userprofile.css" />
<link rel="stylesheet" href="/paging.css" />
<link rel="stylesheet" href="https://code.jquery.com/ui/1.10.3/themes/start/jquery-ui.css" />
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src="https://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script type="text/javascript" src="/paging.js"></script>
<script>
	var user = <%=ts%>;
	var orders = <%=order_list%>;
	var hotels = <%=hotel_list%>;
	var gr = <%=group%>;
	gr = (gr[0] == 'V' ? "VIP" : "Basic");
	var ac = <%=access%>;
	switch (ac[0])
	{
		case 'a': ac = "Administrator";break;
		case 'm': ac = "Sales Manager";break;
		default: ac = "Customer";
	}
</script>
<script type="text/javascript" src="userprofile.js"></script>

<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
</head>

<body>

<div id="header-wrapper">
<div id="header" class="container">
	<div id="logo">
    	<h1><a href="#">Accommodation</a></h1>
    </div>
    <%

            String b = (String)session.getAttribute("auth");
            if(b!=null && b.equals("true"))
            {

                out.print("<div id=\"auth\"><form>" +
				"Welcome, <a href='/user/userProfile.jsp'>"+ 
				user.getName() + "</a> | " +
                                  "<a href=\"/logout.jsp\">Log out</a>" +
						  "</form></div>\n");
            }

        else
        out.print("<div id=\"auth\">" +
                                  "<form method=\"post\" action=\"Auth\">"+
                          "        <input size=\"20\" type=\"text\" placeholder=\"e-mail\" name=\"username\">\n" +

                          "        <input size=\"15\" type=\"password\" placeholder=\"password\" name=\"password\">\n" +
                          "        <input type=\"submit\" value=\"Log in\"> \n" +
						  "        <a href=\"/openid?op=Google\"><img src=\"google_logo.jpg\" id=\"openid-image\" title=\"Login via OpenID account\"></a> \n" + 
                          "        <a id=\"regist\" href=\"register.jsp\">Register</a>\n" +
                                "</form>"+
						  "        <br clear=\"all\"><a id=\"reset_pass\" href=\"resetPassword.jsp\">Forgot your password?</a>\n" +
                          "    </div>\n");
    %>


</div>
</div>

<div id="menu-wrapper">
<div id="menu" class="container">
<%



	if(b!=null && b.equals("true") && user != null)
	{
		User.AccessLevel accessLevel = user.getAccessLevelId();
		if (accessLevel == User.AccessLevel.administrator)
		{
			out.print("<ul>\n"+
			 "<li><a href=\"/main_page.jsp\">Main page</a></li>\n" + 
			 "<li class=\"active\"><a href=\"/user/userProfile.jsp\">Profile</a></li>\n" +
			 "<li><a href=\"/admin/dashboard.jsp\">Admin's dashboard</a></li>\n" +
				"</ul>\n");
		}
		else if (accessLevel == User.AccessLevel.manager)
		{
			out.print("<ul>\n"+
			 "<li><a href=\"/main_page.jsp\">Main page</a></li>\n" + 
			 "<li class=\"active\"><a href=\"/user/userProfile.jsp\">Profile</a></li>\n" +
			 "<li><a href=\"/manager/discounts.jsp\">Comissions and discounts</a></li>\n" +
			 "<li><a href=\"/manager/promocodes.jsp\">Promo-codes</a></li>\n" +
			 "<li><a href=\"/manager/vipstatus.jsp\">VIP-status</a></li>\n" +
			 "<li><a href=\"/manager/reports.jsp\">Reports</a></li>\n" +
				"</ul>\n");
		}
		else
		{
			out.print("<ul>\n"+
			 "<li><a href=\"/main_page.jsp\">Main page</a></li>\n" + 
			 "<li class=\"active\"><a href=\"/user/userProfile.jsp\">Profile</a></li>\n" +
			 "</ul>\n");
		}
	}
%>
</div>
</div>

<div id="content-wrapper">
<div id="content" class="container">
	<header>User profile</header>
    <p>Click on the black information to change it.</p>
    <table id = "userdata">

    </table>
    <div id="link"><a href='javascript:changePassword()'>Change password</a></div>
    <div id="dialog" hidden ></div>
</div>
</div>

<div id="order-wrapper">
<div id="orders" class="container">
    <header>Order history: <span type="text" id="order-count"></span></header>
    <div id="order-table"><table>
    	<thead><tr>
        	<td>#</td>
            <td style="width:350px !important">Hotel</td>
            <td>Price</td>
            <td>Bonus gained</td>
            <td>Adults</td>
            <td>Children</td>
            <td>Nights</td>
            <td>Order date</td>
            <td>Check-in date</td>
            <td>Check-out date</td>
        </tr></thead>
        <tbody id="order_body">

        </tbody>
    </table></div>
    <div id="pageNavPosition" class="pageNavigation"></div>
</div>
</div>

<div id="copyright-wrapper">
<div id="copyright" class="container">
<p>Copyright (c) 2013. All rights reserved.</p>
</div>
</div>

</body>

</html>
